package com.gitee.wsl.compose.ui.popup

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import com.gitee.wsl.compose.ui.base.ActionSheetImpl
import com.gitee.wsl.compose.ui.base.AlertActionsScope
import com.gitee.wsl.compose.ui.base.Popup
import androidx.compose.material3.HorizontalDivider

/**
 * Compose alert dialog with iOS action sheet style.
 *
 * @param onDismissRequest called when dialog is already dismissed. Must not be ignored
 * @param title alert dialog title
 * @param message alert dialog message
 * @param containerColor not used in native dialog
 * @param secondaryContainerColor not used in native dialog
 * @param properties dialog properties
 * @param buttons actions builder block
 *
 */
@Composable
fun Popup.ActionSheet(
    visible: Boolean,
    onDismissRequest: () -> Unit,
    title: (@Composable () -> Unit)? = null,
    message: (@Composable () -> Unit)? = null,
    containerColor: Color = MaterialTheme.colorScheme.primaryContainer,
    secondaryContainerColor: Color = MaterialTheme.colorScheme.secondary,
    properties: DialogProperties = DialogProperties(),
    content: (@Composable () -> Unit)? = null,
    buttons: AlertActionsScope.() -> Unit,
) {
    AnimatedSheet(
        visible = visible,
        onDismissRequest = onDismissRequest,
        properties = properties,
    ) {
        Box(
            Modifier
                .widthIn(max = 500.dp)
                .align(Alignment.BottomCenter)
        ) {
            val hasTitle = title != null || message != null

            val scope = ActionSheetImpl(
                hasTitle = hasTitle,
                primaryContainerColor = containerColor,
                secondaryContainerColor = secondaryContainerColor,
            ).apply(buttons)

            scope.run {
                Content {
                    Column {
                        if (hasTitle) {
                            Column(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(
                                        paddingValues = if (message != null && title != null)
                                            PaddingValues(12.dp)
                                        else PaddingValues(
                                            top = 12.dp,
                                            start = 12.dp,
                                            end = 12.dp,
                                            bottom = 24.dp
                                        )
                                    ),
                                horizontalAlignment = Alignment.CenterHorizontally,
                                verticalArrangement = Arrangement
                                    .spacedBy(6.dp)
                            ) {
                                CompositionLocalProvider(
                                    LocalContentColor provides MaterialTheme.colorScheme.secondary
                                ) {
                                    if (title != null) {
                                        ProvideTextStyle(
                                            MaterialTheme.typography.titleMedium.copy(
                                                fontWeight = if (message != null)
                                                    FontWeight.SemiBold
                                                else FontWeight.Normal,
                                                textAlign = TextAlign.Center,
                                            )
                                        ) {
                                            title()
                                        }
                                    }
                                    if (message != null) {
                                        ProvideTextStyle(
                                            MaterialTheme.typography.titleMedium.copy(
                                                textAlign = TextAlign.Center,
                                                fontWeight = FontWeight.Normal
                                            )
                                        ) {
                                            message()
                                        }
                                    }
                                }
                            }
                        }
                        if (content != null) {
                            if (hasTitle) {
                                HorizontalDivider()
                            }
                            /*CompositionLocalProvider(
                                LocalContainerColor provides containerColor,
                                content = content
                            )*/
                            content()
                        }
                    }
                }

            }
        }
    }
}